package com.huluobo.hyj.hyjmodel.widet

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.util.Log
import android.view.MotionEvent
import android.view.View
import com.huluobo.hyj.hyjmodel.R
import org.jetbrains.anko.sp

/** 字幕条 使用固定宽高，不需要测量
 * 自定义View 三个方法
 *
 *
 */
class SlideBar(context:Context,attrs:AttributeSet) : View(context,attrs) {
    companion object{

        private val SECTIONS = arrayOf(
            "A",
            "B",
            "C",
            "D",
            "E",
            "F",
            "G",
            "H",
            "I",
            "J",
            "K",
            "L",
            "M",
            "N",
            "O",
            "P",
            "Q",
            "R",
            "S",
            "T",
            "U",
            "V",
            "W",
            "X",
            "Y",
            "Z"
        )
    }
    //生成画笔
    private var paint = Paint()

    init {
        paint.apply {
            color = resources.getColor(R.color.qq_section_text_color)
            textSize = sp(12).toFloat() //强转
            //设置画笔的居中对齐
            textAlign = Paint.Align.CENTER
        }
    }

    var sectionHeight = 0f //给第一个item的高U盾一个初始值
    //定义文字的基准线
    var textBaseline = 0f


    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        //计算每个字符分配的高度
        sectionHeight = h*1.0f/ SECTIONS.size

        val fontMetrice = paint.fontMetrics
        //计算绘制文本的高度
        //descent 代表文字的底部现，ascent代表文字的顶部线
        val textHeight  = fontMetrice.descent - fontMetrice.ascent
        //计算基准线
        textBaseline = sectionHeight/2+(textHeight/2-fontMetrice.descent)
    }

    override fun onDraw(canvas: Canvas?) {
        //绘制所有的字符
        val x = width * 1.0f / 2 // 绘制的中心点
        var bseline = textBaseline
        SECTIONS.forEach {
            canvas?.drawText(it,x,bseline,paint) //画出一个字符的位置
            //更新Y 绘制下一个
            bseline += sectionHeight
        }
    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        when(event?.action){
            //触摸时点击
            MotionEvent.ACTION_DOWN -> {
                setBackgroundColor(R.drawable.bg_slide_bar)
                //找到点击的字母
                val index = getTouchIndex(event)
                val firstLetter = SECTIONS[index]
                Log.i("当前触摸的字母：","$firstLetter")
                onSectionChangeListener?.onSectionChange(firstLetter)
            }

            //触摸时移动触发
            MotionEvent.ACTION_MOVE->{
                val index = getTouchIndex(event)
                val firstLetter = SECTIONS[index]
                Log.i("当前触摸的字母：","$firstLetter")
                onSectionChangeListener?.onSectionChange(firstLetter)
            }
            //向上移动
            MotionEvent.ACTION_UP -> {
                setBackgroundColor(Color.TRANSPARENT)
                onSectionChangeListener?.onSlidFinish()
            } //设置背景色
        }
        return true //消费事件
    }

    private fun getTouchIndex(event: MotionEvent): Int {
        Log.i("当前触摸的字母","${event.y},sectionHeight:$sectionHeight")
        //获取下标的原理
        //越界的检查
        var index = (event.y/ sectionHeight).toInt()
        if(index< 0){
            index = 0
        }else if(index >= SECTIONS.size){
            index = SECTIONS.size - 1
        }
        return index
    }
    //第一步定义接口
    interface OnSectionChangeListener{
        fun onSectionChange(firstLetter:String)//显示首字符
        fun onSlidFinish()//滑动结束时回调
    }
    //定义接口回调
    //定义接口变量
    var onSectionChangeListener : OnSectionChangeListener?=null
    //3分别在不同情况触发接口方法
}